package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@Tag(name = "商品分类管理")
@RequestMapping("category")
public class CategoryController extends BaseController {

    @Resource
    private CategoryService categoryService;

    @Operation(summary = "内部调用接口：查询分类的嵌套数据")
    @GetMapping("listCatesTree")
    public R<List<Category>>  listCatesTree(){
        return R.ok(categoryService.listCatesTree());
    }


    @Operation(summary = "根据父id查询下一级分类")
    @GetMapping("listByPid/{pid}")
    public AjaxResult listByPid(@PathVariable("pid") Long pid) {
        return success(categoryService.listByPid(pid));
    }

    @Operation(summary ="导入分类" )
    @PostMapping("import")
    public AjaxResult importCates(MultipartFile file){

        return toAjax(categoryService.importCates(file));
    }
    @Operation(summary ="导出分类" )
    @PostMapping("export")
    public void exportCates(HttpServletResponse response){
        categoryService.exportCates(response);
    }


    @Operation(summary = "删除分类")
    @DeleteMapping("{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {
        //数据校验  只能删除没有下一级的分类
        categoryService.checkHasChildById(id);
        return toAjax(categoryService.removeById(id));
    }
}
